<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/25
 * Time: 13:31
 */

namespace hotel\common\models;

use Yii;
use hotel\common\utils\Tool;

class App extends BaseModel
{
    /**
     * @return string
     */
    public static function tableName()
    {
        return 'su_common_app';
    }

    public function rules()
    {
        return [
            ['app_name', 'required', 'message' => 'app_name不能为空', 'on' => 'add'],
            ['app_name', 'string', 'message' => 'app_name必须是字符串',
                'min' => 2, 'tooShort' => 'app_name最短2位',
                'max' => 16, 'tooLong' => 'app_name最长16位'],
            ['appid', 'default', 'value' => ''],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                'min' => 18, 'tooShort' => 'appid最短18位',
                'max' => 30, 'tooLong' => 'appid最长30位'],
            ['appsecret', 'default', 'value' => ''],
            ['appsecret', 'string', 'message' => 'appsecret必须是字符串',
                'min' => 32, 'tooShort' => 'appsecret最短32位',
                'max' => 32, 'tooLong' => 'appsecret最长32位'],
            ['comid', 'required', 'message' => 'comid不能为空'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                'min' => 8, 'tooShort' => 'comid最短8位',
                'max' => 8, 'tooLong' => 'comid最长8位'],
            ['state', 'default', 'value' => 0],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(0, 1, 2), 'message' => 'state值必须在0,1,2中选择'],
            ['app_info_id', 'required', 'message' => 'app_info_id不能为空'],
            ['app_info_id', 'integer', 'message' => 'app_info_id必须为数字，或数字字符串',
                'min'=>1, 'tooSmall' => 'app_info_id最小存储1',
                'max'=>65535, 'tooBig' => 'app_info_id最大存储65535'],
            ['close_reason', 'default', 'value' => ''],
            ['close_reason', 'string', 'message' => 'close_reason必须是字符串'],
            ['expire_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['expire_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'expire_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],

            ['pay_channel_mer_id', 'string', 'message' => 'pay_channel_mer_id必须为字符串'],
            ['pay_channel_mer_id', 'default', 'value' => ''],

            ['pay_app_id', 'integer', 'message' => 'pay_app_id必须为数字'],
            ['pay_app_id', 'default', 'value' => 0],

            ['pay_channel_id', 'string', 'message' => 'pay_channel_id必须为字符串'],
            ['pay_channel_id', 'default', 'value' => ''],

            ['pay_aid', 'integer', 'message' => 'pay_aid必须为数字'],
            ['pay_aid', 'default', 'value' => 0],

            ['pay_key', 'string', 'message' => 'pay_key必须为字符串'],
            ['pay_key', 'default', 'value' => ''],

            ['ali_pay_channel_mer_id', 'string', 'message' => 'ali_pay_channel_mer_id必须为字符串'],
            ['ali_pay_channel_mer_id', 'default', 'value' => ''],

            ['ali_pay_app_id', 'integer', 'message' => 'ali_pay_app_id必须为数字'],
            ['ali_pay_app_id', 'default', 'value' => 0],

            ['ali_pay_aid', 'integer', 'message' => 'ali_pay_aid必须为数字'],
            ['ali_pay_aid', 'default', 'value' => 0],

            ['ali_pay_key', 'string', 'message' => 'ali_pay_key必须为字符串'],
            ['ali_pay_key', 'default', 'value' => ''],
            ];
    }


    /*添加*/
    public function addApp($data)
    {
        // P($data);
        $newData['App'] = $data;
        $newData['App']['appid'] = 'HOTEL' . date('Y', time()) . Tool::generateRandomStr(3) . date('m', time()) . Tool::generateRandomStr(2) . date('d', time());
        $newData['App']['appsecret'] = md5('HOTEL' . $newData['App']['appid']);
        $newData['App']['app_info_id'] = $newData['App']['appInfoId'];
        $newData['App']['expire_time'] = date('Y-m-d 00:00:00',strtotime('+' . $newData['App']['buyTime'] . 'year +1day'));
        $newData['App']['add_time'] = date('Y-m-d H:i:s', time());
        // P($newData);
        if($this->load($newData) and $this->validate()){
            if($this->save(false)){
                return ['appid' => $newData['App']['appid'], 'appsecret' => $newData['App']['appsecret']];
            }
        }else{
            return $this->errors;
        }
        return false;
    }


    /*
    * 实例列表 （通用）
    * whereArr 条件
    * params 基本参数 包含 field order page limit
    * extends  扩展信息 一些与产品相关的信息
    *
*/
    public static function AppList($whereArr,$params=array(),$extend=array())
    {
        $where = isset($whereArr['where'])?$whereArr['where']:[];
        $and = isset($whereArr['and'])?$whereArr['and']:[];

        $model  = self::find();
        $models = self::queryFormart($model,$where,$params,$and);
        $model  = $models['model'];
        self::$pages = $models['pages'];

        $data  = $model->asarray()->all();
        if(!$data) return array();
        return $data;
    }

}